/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.NguoiDungDAO;
import java.awt.Component;
import java.util.Date;
import javax.swing.JOptionPane;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import pojo.NguoiDung;

/**
 *
 * @author softviet
 */
public class TaiKhoanGUI extends javax.swing.JDialog {

    /**
     * Creates new form TaiKhoanGUI
     */
    private JErrorProvider errorProvider;
    private NguoiDung nguoiDung;
    private static Logger logger=Logger.getLogger(TaiKhoanGUI.class);
    private NguoiDung oldNguoiDung;
    
    public TaiKhoanGUI(java.awt.Frame parent, boolean modal,NguoiDung nguoiDung) {
        super(parent, modal);
        initComponents();
        errorProvider=new JErrorProvider();
        this.nguoiDung=nguoiDung;
        oldNguoiDung=new NguoiDung();
        oldNguoiDung.setUsername(nguoiDung.getUsername());
        oldNguoiDung.setPassword(nguoiDung.getPassword());
        SetTabOrder();
        SetDataToControl();
    }
    
    private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtUsername,txtPassword,txtXacNhanPassword,btnLuu,btnThoat});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }
    
    private void SetDataToControl(){
        txtUsername.setText(nguoiDung.getUsername());
        if(!txtUsername.getText().isEmpty()){
            txtUsername.setEditable(false);
        }
        else{
            txtUsername.setEditable(true);
        }
    }
    
    private int CheckDataBeforeLuu(){
        int dem=0;
        errorProvider.clear();
        if(txtUsername.getText().trim().isEmpty()){
            errorProvider.setError(txtUsername, "required");
            dem++;
        }
        if(txtPassword.getText().compareTo(txtXacNhanPassword.getText())!=0){
            errorProvider.setError(txtXacNhanPassword, "Xác nhận password không chính xác");
            dem++;
        }
        return dem;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        txtXacNhanPassword = new javax.swing.JPasswordField();
        btnLuu = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tài khoản");
        setResizable(false);

        jLabel1.setText("Username");

        jLabel2.setText("Password");

        jLabel3.setText("Xác nhận Password");

        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtXacNhanPassword)
                    .addComponent(txtPassword)
                    .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(30, 30, 30))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(123, Short.MAX_VALUE)
                .addComponent(btnLuu)
                .addGap(18, 18, 18)
                .addComponent(btnThoat)
                .addGap(109, 109, 109))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtXacNhanPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLuu)
                    .addComponent(btnThoat))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        try {
            // TODO add your handling code here:
            if(CheckDataBeforeLuu()>0){
                return;
            }
            nguoiDung.setUsername(txtUsername.getText());
            nguoiDung.setPassword(DigestUtils.md5Hex(txtPassword.getText()));
            NguoiDungDAO.SuaTaiKhoan(nguoiDung);
            JOptionPane.showMessageDialog(this, "Cập nhật thành công");
            dispose();
        } catch (Exception ex) {
            nguoiDung.setUsername(oldNguoiDung.getUsername());
            nguoiDung.setPassword(oldNguoiDung.getPassword());
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnLuuActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    private javax.swing.JPasswordField txtXacNhanPassword;
    // End of variables declaration//GEN-END:variables
}
